// mock数据入口文件
import { getUserList, searchUsers } from './system/user'
import { getProjectList, getProjectPage, getProjectById, createProject, updateProject, deleteProject, getProjectStatistics } from './pypm/project'

// 导出所有mock数据和方法
export const mockData = {
  system: {
    user: {
      getUserList,
      searchUsers
    }
  },
  pypm: {
    project: {
      getProjectList,
      getProjectPage,
      getProjectById,
      createProject,
      updateProject,
      deleteProject,
      getProjectStatistics
    }
  }
}

// 模拟API响应延迟
const delay = (ms = 500) => new Promise(resolve => setTimeout(resolve, ms))

// Mock拦截器配置
export const setupMock = (service: any) => {
  // 用户相关接口
  service.interceptors.request.use(async (config: any) => {
    // 用户列表接口
    if (config.url?.includes('/system/user/list') && config.method === 'get') {
      await delay()
      const users = getUserList()
      return Promise.resolve({
        data: {
          code: 200,
          msg: '操作成功',
          data: users
        }
      })
    }

    // 搜索用户接口
    if (config.url?.includes('/system/user/search') && config.method === 'get') {
      await delay()
      const keyword = config.params?.keyword || ''
      const users = searchUsers(keyword)
      return Promise.resolve({
        data: {
          code: 200,
          msg: '操作成功',
          data: users
        }
      })
    }

    // 项目相关接口
    if (config.url?.includes('/pypm/project/page') && config.method === 'get') {
      await delay()
      const pageData = getProjectPage(config.params)
      return Promise.resolve({
        data: {
          code: 200,
          msg: '操作成功',
          data: pageData
        }
      })
    }

    if (config.url?.includes('/pypm/project/get') && config.method === 'get') {
      await delay()
      const id = config.params?.id || ''
      const project = getProjectById(id)
      return Promise.resolve({
        data: {
          code: 200,
          msg: '操作成功',
          data: project
        }
      })
    }

    if (config.url?.includes('/pypm/project/create') && config.method === 'post') {
      await delay()
      const project = createProject(config.data)
      return Promise.resolve({
        data: {
          code: 200,
          msg: '创建成功',
          data: project
        }
      })
    }

    if (config.url?.includes('/pypm/project/update') && config.method === 'put') {
      await delay()
      const id = config.params?.id || ''
      const project = updateProject(id, config.data)
      return Promise.resolve({
        data: {
          code: 200,
          msg: '更新成功',
          data: project
        }
      })
    }

    if (config.url?.includes('/pypm/project/delete') && config.method === 'delete') {
      await delay()
      const id = config.params?.id || ''
      const success = deleteProject(id)
      return Promise.resolve({
        data: {
          code: 200,
          msg: success ? '删除成功' : '删除失败',
          data: success
        }
      })
    }

    if (config.url?.includes('/pypm/project/statistics') && config.method === 'get') {
      await delay()
      const statistics = getProjectStatistics()
      return Promise.resolve({
        data: {
          code: 200,
          msg: '操作成功',
          data: statistics
        }
      })
    }

    // 如果不是mock的接口，继续发送请求
    return config
  }, (error: any) => {
    return Promise.reject(error)
  })

  return service
}